package com.zb.framework.service.impl;

import com.baomidou.dynamic.datasource.creator.DruidDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.druid.DruidConfig;
import com.zb.framework.model.LdapOu;
import com.zb.framework.model.LdapUser;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.List;

/**
 * @author fujianjian
 * @date 2023/5/23 11:23
 **/
@Service
public class IhrDatasourceServiceImpl implements InitializingBean {

    private String url = "jdbc:mysql://10.89.2.119:3306/ihr_core?" +
            "useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    private String username = "ihr_read_gly";
    private String password = "Ihr@zb$2021";

    private String jj_url = "jdbc:mysql://10.99.2.11:3306/ihr_core?" +
            "useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    private String jj_username = "ihr_master";
    private String jj_password = "iHR@zhengB!2021";

    private static DataSource DATA_SOURCE;

    public List<LdapUser> fetchWhaleUser() {
        StringBuilder sql = new StringBuilder();
        sql.append(" select");
        sql.append(" ee.employee_id employeeId,");
        sql.append(" ee.employee_code employeeCode,");
        sql.append(" ee.employee_name employeeName,");
        sql.append(" ee.id_number idNumber,");
        sql.append(" ee.mobile mobile,");
        sql.append(" ee.login_id loginId,");
        sql.append(" ou.unit_id unitId,");
        sql.append(" ou.unit_code unitCode,");
        sql.append(" ou.unit_name unitName,");
        sql.append(" ou.unit_path_name unitPathName,");
        sql.append(" pjl.job_level_id jobLevelId,");
        sql.append(" pjl.job_level_name jobLevelName,");
        sql.append(" pg.grade_id gradeId,");
        sql.append(" pg.grade_name gradeName,");
        sql.append(" pp.position_id positionId,");
        sql.append(" pp.position_code positionCode,");
        sql.append(" pp.position_name positionName");
        // sql.append(" su.pwd password");
        sql.append(" from t_emp_employee ee");
        sql.append(" left join t_org_unit ou on ee.unit_id = ou.unit_id");
        sql.append(" left join t_pos_job_level pjl on ee.job_level_id = pjl.job_level_id");
        sql.append(" left join t_pos_grade pg on ee.job_grade = pg.grade_id");
        sql.append(" left join t_pos_position pp on ee.position_id = pp.position_id");
        // sql.append(" left join t_sys_user su on ee.login_id=su.login_id");
        // sql.append(" where ou.`path`  like '80037680_82014696%'");
        sql.append(" where ou.`path`  like '80037680_82014696_%'");
        // sql.append(" and ee.login_id='shikai02'");
        //

        JdbcTemplate jdbcTemplate = new JdbcTemplate(DATA_SOURCE);
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(LdapUser.class));
    }

    public List<LdapUser> fetchShanlinUser() {
        StringBuilder sql = new StringBuilder();
        sql.append(" select");
        sql.append(" ee.employee_id employeeId,");
        sql.append(" ee.employee_code employeeCode,");
        sql.append(" ee.employee_name employeeName,");
        sql.append(" ee.id_number idNumber,");
        sql.append(" ee.mobile mobile,");
        sql.append(" ee.login_id loginId,");
        sql.append(" ou.unit_id unitId,");
        sql.append(" ou.unit_code unitCode,");
        sql.append(" ou.unit_name unitName,");
        sql.append(" ou.unit_path_name unitPathName,");
        sql.append(" pjl.job_level_id jobLevelId,");
        sql.append(" pjl.job_level_name jobLevelName,");
        sql.append(" pg.grade_id gradeId,");
        sql.append(" pg.grade_name gradeName,");
        sql.append(" pp.position_id positionId,");
        sql.append(" pp.position_code positionCode,");
        sql.append(" pp.position_name positionName");
        // sql.append(" su.pwd password");
        sql.append(" from t_emp_employee ee");
        sql.append(" left join t_org_unit ou on ee.unit_id = ou.unit_id");
        sql.append(" left join t_pos_job_level pjl on ee.job_level_id = pjl.job_level_id");
        sql.append(" left join t_pos_grade pg on ee.job_grade = pg.grade_id");
        sql.append(" left join t_pos_position pp on ee.position_id = pp.position_id");
        // sql.append(" left join t_sys_user su on ee.login_id=su.login_id");
        // sql.append(" where ou.`path`  like '80037680_82014696%'");
        sql.append(" where ou.`path`  like '80000001_80030015_82010921_80037278_%'");
        // sql.append(" and ee.login_id='shikai02'");
        //

        JdbcTemplate jdbcTemplate = new JdbcTemplate(DATA_SOURCE);
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(LdapUser.class));
    }


    public List<LdapOu> fetchLdapOu() {
        StringBuilder sb = new StringBuilder();
        sb.append(" select ou.unit_id unitId,");
        sb.append("       ou.unit_name uniteName,");
        sb.append("        ou.unit_path_name unitPathName");
        sb.append(" from t_org_unit ou ");
        // sb.append(" where ou.`path` like '80037680_82014696' order by ou.`path`");
        sb.append(" where ou.`path` like '80037680_82014696_82015467_%' order by ou.`path`");
        // sb.append(" limit 1");

        JdbcTemplate jdbcTemplate = new JdbcTemplate(DATA_SOURCE);
        return jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<>(LdapOu.class));
    }

    public static void main0(String[] args) {
        List<LdapUser> list = new IhrDatasourceServiceImpl().fetchWhaleUser();
        System.out.println("1111111111111111111>" + list.size());

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        DataSourceProperty property = new DataSourceProperty();
        property.setUrl(url);
        property.setUsername(username);
        property.setPassword(password);
        // property.setUrl(jj_url);
        // property.setUsername(jj_username);
        // property.setPassword(jj_password);
        DruidDataSourceCreator creator = new DruidDataSourceCreator(new DruidConfig());
        DATA_SOURCE = creator.createDataSource(property);
    }
}
